// pages/index/index.js
const netWorked = require('../../utils/netWork.js')
Page({

     /**
      * 页面的初始数据
      */
     data: {
          detailValue:'',//用户名
          detailValue2:'',//密码
          select:false,//登陆方式是否显示
          tihuoWay:"操作员登陆",
          indexId:0,//登陆方式的代号
          selects:true,//空白占位
          isShow:true,//登陆界面
          isShowlogin:false,//注册
          formName:"",
          phone:'',
          tim:'',
          wxs:"",
          site:"",
          password:'',
          password2: ''

     },
     bindKeyInput(e){
          let detailValue = e.detail.value
         this.setData({
              detailValue
         })
     },
     bindKeyInput2(e) {
          let detailValue2 = e.detail.value
          this.setData({
               detailValue2
          })
      },
     sumtype(){
          let detailValue=this.data.detailValue
          let detailValue2 = this.data.detailValue2
          let indexId = this.data.indexId
          let that =this;
          if (detailValue=="" || detailValue2==""){
               wx.showToast({
                    title: '请输入用户名或密码',
                    icon: "none",
               })
          }else{
               netWorked.netWork({
                    url: 'Login/Login',
                    data: {
                         userType: indexId,
                         userid: detailValue,
                         pwd: detailValue2
                    },
                    method: "POST",
                    success: function (res) {
                         // let that =this
                         // let ghod = res.data[0].grant.length
                      console.log(res)
                         console.log(res)
                         if (res.data == 0) {
                              wx.showToast({
                                   title: '用户名或密码错误',
                                   icon: "none",
                              })

                         } else if (res.data[0].grant.length > 1) {
                              wx.showToast({
                                   title: '登陆成功',
                                   icon: "none",
                              })
                           console.log(res.data["0"].id+"sssd")
                           wx.setStorageSync("role_id", res.data["0"].role_id)
                           wx.setStorageSync("loginid", res.data["0"].id)//id
                           wx.setStorageSync("loginname", res.data["0"].name)//id
                           wx.setStorageSync("grant", res.data["0"].grant)//权限
                           wx.setStorageSync("branch_no", res.data["0"].branch_no)//门店
                           wx.setStorageSync("userType", res.data["0"].userType)//登录类型
                              wx.switchTab({
                                   url: '/pages/home/index',
                              })
                          
                           
                         }

                    }
               })
          }
     },
     bindShowMsg() {
          this.setData({
               select: !this.data.select,
               selects: !this.data.selects
          })
     },
     mySelect(e) {
          var name = e.currentTarget.dataset.value
          let indexId = e.currentTarget.dataset.name
          this.setData({
               tihuoWay: name,
               select: false,
               selects:true,
               indexId
          })
          // console.log(this.data.tihuoWay)
     },
     register(e){
          this.setData({
               isShow: false,
               isShowlogin: true
          })
     },
     // 注册取值
     fromInput(e){
          let formName = e.detail.value
          this.setData({
               formName
          })
       
     },
     fromInputP(e) {
          let phone = e.detail.value
          this.setData({
               phone
          })
      
     },
     fromInput1(e) {
          let tim = e.detail.value
          this.setData({
               tim
          })
        
     },
     fromInput2(e) {
          let wxs = e.detail.value
          this.setData({
               wxs
          })
         
     },
     fromInput3(e) {
          let site = e.detail.value
          this.setData({
               site
          })
         
     },
     fromInput4(e) {
          let password = e.detail.value
          this.setData({
               password
          })
     
     },
     fromInput5(e){
          let password2 = e.detail.value
          this.setData({
               password2
          })
        
     },
     // 注册安扭事件
     register2(e){
          let formName = this.data.formName
          let phone = this.data.phone
          let tim = this.data.tim
          let wxs = this.data.wxs
          let site = this.data.site
          let password = this.data.password
          let password2 = this.data.password2
          let pattern = /^1[34578]\d{9}$/;
          let that = this
          if (formName == "" || phone == "" || password==""){
               wx.showToast({
                    title: '必填项不能为空',
                    icon: "none",
               })
          } else if (password2 == ""){
               wx.showToast({
                    title: '请确认密码',
                    icon: "none",
               })
          } else if (!pattern.test(phone)){
               wx.showToast({
                    title: '请输入正确的手机号',
                    icon: "none",
               })
          }else{
            netWorked.netWork({
                    url: '/Login/Register',
                    data: {
                         Name: formName,
                         phone: phone,
                         qq:tim,
                         WeChat:wxs,
                         address: site,
                         pwd1: password
                    },
                    method: "POST",
                    success: function (res) {  
                         if (res.data==1){
                              wx.showToast({
                                   title: '注册成功',
                                   icon: "none",
                              })
                              that.setData({
                                   isShow:true 
                              })
                         } else if (res.data == 0)  {
                              wx.showToast({
                                   title: '该账户以经注册，请去登录',
                                   icon: "none",
                              })
                         } else{
                              wx.showToast({
                                   title: '注册失败',
                                   icon: "none",
                              })
                         }              
                         console.log(res.data)
                    }
               })
          }
     },
     sumtyub2(){
          this.setData({
               isShow: true ,
               isShowlogin:false
          })
     },
     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {

     },

     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function () {

     },

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function () {

     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function () {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function () {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {

     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     }
})